package com.abo.yaas.domain;

import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSeeAlso;

@SuppressWarnings("serial")
@Entity
@Table(name = "TBL_BID")
@XmlRootElement
@XmlSeeAlso({ User.class })
public class Bid extends YaasPojo {
    private Long auctionId;

    private Date time = new Date();

    private BigDecimal amount = new BigDecimal(1l);

    private User user;

    public Bid() {
    }

    public Bid(final User user, final Long auctionId) {
        setUser(user);
        setAuctionId(auctionId);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null || !(obj instanceof Bid)) {
            return false;
        }

        if (obj == this) {
            return true;
        }

        if (getId() == null) {
            return super.equals(obj);
        }

        return getId().equals(((Bid) obj).getId());
    }

    @Column(name = "Amount")
    public BigDecimal getAmount() {
        return amount;
    }

    @Column(name = "auctionID")
    public Long getAuctionId() {
        return auctionId;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "Time")
    public Date getTime() {
        return time;
    }

    @ManyToOne
    @JoinColumn(name = "UserID", nullable = false)
    @XmlElementRef(name = "User")
    public User getUser() {
        return user;
    }

    @Transient
    public String getUserName() {
        if (user == null) {
            return "";
        }
        return user.getLogin();
    }

    @Override
    public int hashCode() {
        int result = 0;
        result += time.hashCode();
        result += user.hashCode();
        result += auctionId.hashCode();
        if (getId() != null) {
            result += getId().hashCode();
        }
        return result;
    }

    public void setAmount(BigDecimal amount) {
        this.amount = amount;
    }

    public void setAuctionId(Long auctionId) {
        this.auctionId = auctionId;
    }

    public void setTime(Date time) {
        this.time = time;
    }

    public void setUser(User user) {
        this.user = user;
    }
}
